
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.contrib.postgres.fields.ranges &#8212; Django 2.2.12.dev20200304094918 documentation</title>
    <link rel="stylesheet" href="../../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../../../" src="../../../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../../search.html" />



 
<script type="text/javascript" src="../../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../../index.html">Django 2.2.12.dev20200304094918 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-contrib-postgres-fields-ranges">
            
  <h1>Source code for django.contrib.postgres.fields.ranges</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">datetime</span>
<span class="kn">import</span> <span class="nn">json</span>

<span class="kn">from</span> <span class="nn">psycopg2.extras</span> <span class="k">import</span> <span class="n">DateRange</span><span class="p">,</span> <span class="n">DateTimeTZRange</span><span class="p">,</span> <span class="n">NumericRange</span><span class="p">,</span> <span class="n">Range</span>

<span class="kn">from</span> <span class="nn">django.contrib.postgres</span> <span class="k">import</span> <span class="n">forms</span><span class="p">,</span> <span class="n">lookups</span>
<span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>

<span class="kn">from</span> <span class="nn">.utils</span> <span class="k">import</span> <span class="n">AttributeSetter</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span>
    <span class="s1">&#39;RangeField&#39;</span><span class="p">,</span> <span class="s1">&#39;IntegerRangeField&#39;</span><span class="p">,</span> <span class="s1">&#39;BigIntegerRangeField&#39;</span><span class="p">,</span>
    <span class="s1">&#39;DecimalRangeField&#39;</span><span class="p">,</span> <span class="s1">&#39;DateTimeRangeField&#39;</span><span class="p">,</span> <span class="s1">&#39;DateRangeField&#39;</span><span class="p">,</span>
    <span class="s1">&#39;FloatRangeField&#39;</span><span class="p">,</span>
<span class="p">]</span>


<div class="viewcode-block" id="RangeField"><a class="viewcode-back" href="../../../../../ref/contrib/postgres/fields.html#django.contrib.postgres.fields.RangeField">[docs]</a><span class="k">class</span> <span class="nc">RangeField</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Field</span><span class="p">):</span>
    <span class="n">empty_strings_allowed</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="c1"># Initializing base_field here ensures that its model matches the model for self.</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;base_field&#39;</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">base_field</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">base_field</span><span class="p">()</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">model</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">[</span><span class="s1">&#39;model&#39;</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">AttributeError</span><span class="p">(</span><span class="s2">&quot;&#39;</span><span class="si">%s</span><span class="s2">&#39; object has no attribute &#39;model&#39;&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">)</span>

    <span class="nd">@model</span><span class="o">.</span><span class="n">setter</span>
    <span class="k">def</span> <span class="nf">model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">[</span><span class="s1">&#39;model&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">model</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">base_field</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">model</span>

    <span class="k">def</span> <span class="nf">get_prep_value</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">value</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">Range</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">value</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)):</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">range_type</span><span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">value</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="k">return</span> <span class="n">value</span>

    <span class="k">def</span> <span class="nf">to_python</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
            <span class="c1"># Assume we&#39;re deserializing</span>
            <span class="n">vals</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">end</span> <span class="ow">in</span> <span class="p">(</span><span class="s1">&#39;lower&#39;</span><span class="p">,</span> <span class="s1">&#39;upper&#39;</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">end</span> <span class="ow">in</span> <span class="n">vals</span><span class="p">:</span>
                    <span class="n">vals</span><span class="p">[</span><span class="n">end</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">base_field</span><span class="o">.</span><span class="n">to_python</span><span class="p">(</span><span class="n">vals</span><span class="p">[</span><span class="n">end</span><span class="p">])</span>
            <span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">range_type</span><span class="p">(</span><span class="o">**</span><span class="n">vals</span><span class="p">)</span>
        <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="p">(</span><span class="nb">list</span><span class="p">,</span> <span class="nb">tuple</span><span class="p">)):</span>
            <span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">range_type</span><span class="p">(</span><span class="n">value</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">value</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="k">return</span> <span class="n">value</span>

    <span class="k">def</span> <span class="nf">set_attributes_from_name</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">set_attributes_from_name</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">base_field</span><span class="o">.</span><span class="n">set_attributes_from_name</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">value_to_string</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
        <span class="n">value</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">value_from_object</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">value</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span>
        <span class="k">if</span> <span class="n">value</span><span class="o">.</span><span class="n">isempty</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">({</span><span class="s2">&quot;empty&quot;</span><span class="p">:</span> <span class="kc">True</span><span class="p">})</span>
        <span class="n">base_field</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">base_field</span>
        <span class="n">result</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;bounds&quot;</span><span class="p">:</span> <span class="n">value</span><span class="o">.</span><span class="n">_bounds</span><span class="p">}</span>
        <span class="k">for</span> <span class="n">end</span> <span class="ow">in</span> <span class="p">(</span><span class="s1">&#39;lower&#39;</span><span class="p">,</span> <span class="s1">&#39;upper&#39;</span><span class="p">):</span>
            <span class="n">val</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">end</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">val</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">result</span><span class="p">[</span><span class="n">end</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">obj</span> <span class="o">=</span> <span class="n">AttributeSetter</span><span class="p">(</span><span class="n">base_field</span><span class="o">.</span><span class="n">attname</span><span class="p">,</span> <span class="n">val</span><span class="p">)</span>
                <span class="n">result</span><span class="p">[</span><span class="n">end</span><span class="p">]</span> <span class="o">=</span> <span class="n">base_field</span><span class="o">.</span><span class="n">value_to_string</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">formfield</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">kwargs</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s1">&#39;form_class&#39;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">form_field</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">formfield</span><span class="p">(</span><span class="o">**</span><span class="n">kwargs</span><span class="p">)</span></div>


<div class="viewcode-block" id="IntegerRangeField"><a class="viewcode-back" href="../../../../../ref/contrib/postgres/fields.html#django.contrib.postgres.fields.IntegerRangeField">[docs]</a><span class="k">class</span> <span class="nc">IntegerRangeField</span><span class="p">(</span><span class="n">RangeField</span><span class="p">):</span>
    <span class="n">base_field</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">IntegerField</span>
    <span class="n">range_type</span> <span class="o">=</span> <span class="n">NumericRange</span>
    <span class="n">form_field</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">IntegerRangeField</span>

    <span class="k">def</span> <span class="nf">db_type</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;int4range&#39;</span></div>


<div class="viewcode-block" id="BigIntegerRangeField"><a class="viewcode-back" href="../../../../../ref/contrib/postgres/fields.html#django.contrib.postgres.fields.BigIntegerRangeField">[docs]</a><span class="k">class</span> <span class="nc">BigIntegerRangeField</span><span class="p">(</span><span class="n">RangeField</span><span class="p">):</span>
    <span class="n">base_field</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">BigIntegerField</span>
    <span class="n">range_type</span> <span class="o">=</span> <span class="n">NumericRange</span>
    <span class="n">form_field</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">IntegerRangeField</span>

    <span class="k">def</span> <span class="nf">db_type</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;int8range&#39;</span></div>


<div class="viewcode-block" id="DecimalRangeField"><a class="viewcode-back" href="../../../../../ref/contrib/postgres/fields.html#django.contrib.postgres.fields.DecimalRangeField">[docs]</a><span class="k">class</span> <span class="nc">DecimalRangeField</span><span class="p">(</span><span class="n">RangeField</span><span class="p">):</span>
    <span class="n">base_field</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DecimalField</span>
    <span class="n">range_type</span> <span class="o">=</span> <span class="n">NumericRange</span>
    <span class="n">form_field</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">DecimalRangeField</span>

    <span class="k">def</span> <span class="nf">db_type</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;numrange&#39;</span></div>


<div class="viewcode-block" id="FloatRangeField"><a class="viewcode-back" href="../../../../../ref/contrib/postgres/fields.html#django.contrib.postgres.fields.FloatRangeField">[docs]</a><span class="k">class</span> <span class="nc">FloatRangeField</span><span class="p">(</span><span class="n">RangeField</span><span class="p">):</span>
    <span class="n">system_check_deprecated_details</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;msg&#39;</span><span class="p">:</span> <span class="p">(</span>
            <span class="s1">&#39;FloatRangeField is deprecated and will be removed in Django 3.1.&#39;</span>
        <span class="p">),</span>
        <span class="s1">&#39;hint&#39;</span><span class="p">:</span> <span class="s1">&#39;Use DecimalRangeField instead.&#39;</span><span class="p">,</span>
        <span class="s1">&#39;id&#39;</span><span class="p">:</span> <span class="s1">&#39;fields.W902&#39;</span><span class="p">,</span>
    <span class="p">}</span>
    <span class="n">base_field</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">FloatField</span>
    <span class="n">range_type</span> <span class="o">=</span> <span class="n">NumericRange</span>
    <span class="n">form_field</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">FloatRangeField</span>

    <span class="k">def</span> <span class="nf">db_type</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;numrange&#39;</span></div>


<div class="viewcode-block" id="DateTimeRangeField"><a class="viewcode-back" href="../../../../../ref/contrib/postgres/fields.html#django.contrib.postgres.fields.DateTimeRangeField">[docs]</a><span class="k">class</span> <span class="nc">DateTimeRangeField</span><span class="p">(</span><span class="n">RangeField</span><span class="p">):</span>
    <span class="n">base_field</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateTimeField</span>
    <span class="n">range_type</span> <span class="o">=</span> <span class="n">DateTimeTZRange</span>
    <span class="n">form_field</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">DateTimeRangeField</span>

    <span class="k">def</span> <span class="nf">db_type</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;tstzrange&#39;</span></div>


<div class="viewcode-block" id="DateRangeField"><a class="viewcode-back" href="../../../../../ref/contrib/postgres/fields.html#django.contrib.postgres.fields.DateRangeField">[docs]</a><span class="k">class</span> <span class="nc">DateRangeField</span><span class="p">(</span><span class="n">RangeField</span><span class="p">):</span>
    <span class="n">base_field</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateField</span>
    <span class="n">range_type</span> <span class="o">=</span> <span class="n">DateRange</span>
    <span class="n">form_field</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">DateRangeField</span>

    <span class="k">def</span> <span class="nf">db_type</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;daterange&#39;</span></div>


<span class="n">RangeField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">lookups</span><span class="o">.</span><span class="n">DataContains</span><span class="p">)</span>
<span class="n">RangeField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">lookups</span><span class="o">.</span><span class="n">ContainedBy</span><span class="p">)</span>
<span class="n">RangeField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">lookups</span><span class="o">.</span><span class="n">Overlap</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">DateTimeRangeContains</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Lookup</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Lookup for Date/DateTimeRange containment to cast the rhs to the correct</span>
<span class="sd">    type.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">lookup_name</span> <span class="o">=</span> <span class="s1">&#39;contains&#39;</span>

    <span class="k">def</span> <span class="nf">process_rhs</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">compiler</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="c1"># Transform rhs value for db lookup.</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rhs</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="p">):</span>
            <span class="n">output_field</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">DateTimeField</span><span class="p">()</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rhs</span><span class="p">,</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">)</span> <span class="k">else</span> <span class="n">models</span><span class="o">.</span><span class="n">DateField</span><span class="p">()</span>
            <span class="n">value</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">Value</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rhs</span><span class="p">,</span> <span class="n">output_field</span><span class="o">=</span><span class="n">output_field</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">rhs</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">resolve_expression</span><span class="p">(</span><span class="n">compiler</span><span class="o">.</span><span class="n">query</span><span class="p">)</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">process_rhs</span><span class="p">(</span><span class="n">compiler</span><span class="p">,</span> <span class="n">connection</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">as_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">compiler</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="n">lhs</span><span class="p">,</span> <span class="n">lhs_params</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">process_lhs</span><span class="p">(</span><span class="n">compiler</span><span class="p">,</span> <span class="n">connection</span><span class="p">)</span>
        <span class="n">rhs</span><span class="p">,</span> <span class="n">rhs_params</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">process_rhs</span><span class="p">(</span><span class="n">compiler</span><span class="p">,</span> <span class="n">connection</span><span class="p">)</span>
        <span class="n">params</span> <span class="o">=</span> <span class="n">lhs_params</span> <span class="o">+</span> <span class="n">rhs_params</span>
        <span class="c1"># Cast the rhs if needed.</span>
        <span class="n">cast_sql</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="k">if</span> <span class="p">(</span>
            <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rhs</span><span class="p">,</span> <span class="n">models</span><span class="o">.</span><span class="n">Expression</span><span class="p">)</span> <span class="ow">and</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">rhs</span><span class="o">.</span><span class="n">_output_field_or_none</span> <span class="ow">and</span>
            <span class="c1"># Skip cast if rhs has a matching range type.</span>
            <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rhs</span><span class="o">.</span><span class="n">_output_field_or_none</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">lhs</span><span class="o">.</span><span class="n">output_field</span><span class="o">.</span><span class="vm">__class__</span><span class="p">)</span>
        <span class="p">):</span>
            <span class="n">cast_internal_type</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">lhs</span><span class="o">.</span><span class="n">output_field</span><span class="o">.</span><span class="n">base_field</span><span class="o">.</span><span class="n">get_internal_type</span><span class="p">()</span>
            <span class="n">cast_sql</span> <span class="o">=</span> <span class="s1">&#39;::</span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">connection</span><span class="o">.</span><span class="n">data_types</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">cast_internal_type</span><span class="p">))</span>
        <span class="k">return</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> @&gt; </span><span class="si">%s%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">lhs</span><span class="p">,</span> <span class="n">rhs</span><span class="p">,</span> <span class="n">cast_sql</span><span class="p">),</span> <span class="n">params</span>


<span class="n">DateRangeField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">DateTimeRangeContains</span><span class="p">)</span>
<span class="n">DateTimeRangeField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">DateTimeRangeContains</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">RangeContainedBy</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Lookup</span><span class="p">):</span>
    <span class="n">lookup_name</span> <span class="o">=</span> <span class="s1">&#39;contained_by&#39;</span>
    <span class="n">type_mapping</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;integer&#39;</span><span class="p">:</span> <span class="s1">&#39;int4range&#39;</span><span class="p">,</span>
        <span class="s1">&#39;bigint&#39;</span><span class="p">:</span> <span class="s1">&#39;int8range&#39;</span><span class="p">,</span>
        <span class="s1">&#39;double precision&#39;</span><span class="p">:</span> <span class="s1">&#39;numrange&#39;</span><span class="p">,</span>
        <span class="s1">&#39;date&#39;</span><span class="p">:</span> <span class="s1">&#39;daterange&#39;</span><span class="p">,</span>
        <span class="s1">&#39;timestamp with time zone&#39;</span><span class="p">:</span> <span class="s1">&#39;tstzrange&#39;</span><span class="p">,</span>
    <span class="p">}</span>

    <span class="k">def</span> <span class="nf">as_sql</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">qn</span><span class="p">,</span> <span class="n">connection</span><span class="p">):</span>
        <span class="n">field</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">lhs</span><span class="o">.</span><span class="n">output_field</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">models</span><span class="o">.</span><span class="n">FloatField</span><span class="p">):</span>
            <span class="n">sql</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">::numeric &lt;@ </span><span class="si">%s</span><span class="s1">::</span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">type_mapping</span><span class="p">[</span><span class="n">field</span><span class="o">.</span><span class="n">db_type</span><span class="p">(</span><span class="n">connection</span><span class="p">)])</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">sql</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> &lt;@ </span><span class="si">%s</span><span class="s1">::</span><span class="si">{}</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">type_mapping</span><span class="p">[</span><span class="n">field</span><span class="o">.</span><span class="n">db_type</span><span class="p">(</span><span class="n">connection</span><span class="p">)])</span>
        <span class="n">lhs</span><span class="p">,</span> <span class="n">lhs_params</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">process_lhs</span><span class="p">(</span><span class="n">qn</span><span class="p">,</span> <span class="n">connection</span><span class="p">)</span>
        <span class="n">rhs</span><span class="p">,</span> <span class="n">rhs_params</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">process_rhs</span><span class="p">(</span><span class="n">qn</span><span class="p">,</span> <span class="n">connection</span><span class="p">)</span>
        <span class="n">params</span> <span class="o">=</span> <span class="n">lhs_params</span> <span class="o">+</span> <span class="n">rhs_params</span>
        <span class="k">return</span> <span class="n">sql</span> <span class="o">%</span> <span class="p">(</span><span class="n">lhs</span><span class="p">,</span> <span class="n">rhs</span><span class="p">),</span> <span class="n">params</span>

    <span class="k">def</span> <span class="nf">get_prep_lookup</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">RangeField</span><span class="p">()</span><span class="o">.</span><span class="n">get_prep_value</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rhs</span><span class="p">)</span>


<span class="n">models</span><span class="o">.</span><span class="n">DateField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">RangeContainedBy</span><span class="p">)</span>
<span class="n">models</span><span class="o">.</span><span class="n">DateTimeField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">RangeContainedBy</span><span class="p">)</span>
<span class="n">models</span><span class="o">.</span><span class="n">IntegerField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">RangeContainedBy</span><span class="p">)</span>
<span class="n">models</span><span class="o">.</span><span class="n">BigIntegerField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">RangeContainedBy</span><span class="p">)</span>
<span class="n">models</span><span class="o">.</span><span class="n">FloatField</span><span class="o">.</span><span class="n">register_lookup</span><span class="p">(</span><span class="n">RangeContainedBy</span><span class="p">)</span>


<span class="nd">@RangeField</span><span class="o">.</span><span class="n">register_lookup</span>
<span class="k">class</span> <span class="nc">FullyLessThan</span><span class="p">(</span><span class="n">lookups</span><span class="o">.</span><span class="n">PostgresSimpleLookup</span><span class="p">):</span>
    <span class="n">lookup_name</span> <span class="o">=</span> <span class="s1">&#39;fully_lt&#39;</span>
    <span class="n">operator</span> <span class="o">=</span> <span class="s1">&#39;&lt;&lt;&#39;</span>


<span class="nd">@RangeField</span><span class="o">.</span><span class="n">register_lookup</span>
<span class="k">class</span> <span class="nc">FullGreaterThan</span><span class="p">(</span><span class="n">lookups</span><span class="o">.</span><span class="n">PostgresSimpleLookup</span><span class="p">):</span>
    <span class="n">lookup_name</span> <span class="o">=</span> <span class="s1">&#39;fully_gt&#39;</span>
    <span class="n">operator</span> <span class="o">=</span> <span class="s1">&#39;&gt;&gt;&#39;</span>


<span class="nd">@RangeField</span><span class="o">.</span><span class="n">register_lookup</span>
<span class="k">class</span> <span class="nc">NotLessThan</span><span class="p">(</span><span class="n">lookups</span><span class="o">.</span><span class="n">PostgresSimpleLookup</span><span class="p">):</span>
    <span class="n">lookup_name</span> <span class="o">=</span> <span class="s1">&#39;not_lt&#39;</span>
    <span class="n">operator</span> <span class="o">=</span> <span class="s1">&#39;&amp;&gt;&#39;</span>


<span class="nd">@RangeField</span><span class="o">.</span><span class="n">register_lookup</span>
<span class="k">class</span> <span class="nc">NotGreaterThan</span><span class="p">(</span><span class="n">lookups</span><span class="o">.</span><span class="n">PostgresSimpleLookup</span><span class="p">):</span>
    <span class="n">lookup_name</span> <span class="o">=</span> <span class="s1">&#39;not_gt&#39;</span>
    <span class="n">operator</span> <span class="o">=</span> <span class="s1">&#39;&amp;&lt;&#39;</span>


<span class="nd">@RangeField</span><span class="o">.</span><span class="n">register_lookup</span>
<span class="k">class</span> <span class="nc">AdjacentToLookup</span><span class="p">(</span><span class="n">lookups</span><span class="o">.</span><span class="n">PostgresSimpleLookup</span><span class="p">):</span>
    <span class="n">lookup_name</span> <span class="o">=</span> <span class="s1">&#39;adjacent_to&#39;</span>
    <span class="n">operator</span> <span class="o">=</span> <span class="s1">&#39;-|-&#39;</span>


<span class="nd">@RangeField</span><span class="o">.</span><span class="n">register_lookup</span>
<span class="k">class</span> <span class="nc">RangeStartsWith</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Transform</span><span class="p">):</span>
    <span class="n">lookup_name</span> <span class="o">=</span> <span class="s1">&#39;startswith&#39;</span>
    <span class="n">function</span> <span class="o">=</span> <span class="s1">&#39;lower&#39;</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">output_field</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">lhs</span><span class="o">.</span><span class="n">output_field</span><span class="o">.</span><span class="n">base_field</span>


<span class="nd">@RangeField</span><span class="o">.</span><span class="n">register_lookup</span>
<span class="k">class</span> <span class="nc">RangeEndsWith</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Transform</span><span class="p">):</span>
    <span class="n">lookup_name</span> <span class="o">=</span> <span class="s1">&#39;endswith&#39;</span>
    <span class="n">function</span> <span class="o">=</span> <span class="s1">&#39;upper&#39;</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">output_field</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">lhs</span><span class="o">.</span><span class="n">output_field</span><span class="o">.</span><span class="n">base_field</span>


<span class="nd">@RangeField</span><span class="o">.</span><span class="n">register_lookup</span>
<span class="k">class</span> <span class="nc">IsEmpty</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Transform</span><span class="p">):</span>
    <span class="n">lookup_name</span> <span class="o">=</span> <span class="s1">&#39;isempty&#39;</span>
    <span class="n">function</span> <span class="o">=</span> <span class="s1">&#39;isempty&#39;</span>
    <span class="n">output_field</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">BooleanField</span><span class="p">()</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 04, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>